package com.zx.video28181.activity;

import android.app.Dialog;
import android.content.Context;
import android.hardware.Camera;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.video28181.R;

import java.util.List;

public class QualityDialog extends Dialog implements View.OnClickListener{

    private VideoLevelCallBack levelCallBack;
    private List<Camera.Size> previewSizes;
    private Context mContext;

    public QualityDialog( Context context) {
        super(context,R.style.DialogTheme);
        this.mContext= context;
        View layout = getLayoutInflater().inflate(R.layout.dialog_layout, null);

        RecyclerView formatLevel= layout.findViewById(R.id.rv_format_level);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(RecyclerView.VERTICAL);
        formatLevel.setLayoutManager(manager);
        formatLevel.setAdapter(new LevelAdapter());
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }




    public void setLevelCallBack(VideoLevelCallBack levelCallBack) {
        this.levelCallBack = levelCallBack;
    }

    public void setPreviewSizes(List<Camera.Size> previewSizes) {
        this.previewSizes = previewSizes;
    }

    @Override
    public void onClick(View v) {
        if(levelCallBack == null) return;

        if(v.getId() == R.id.tv_1080p){
            levelCallBack.setVideoLevel(1);
        }else if(v.getId() == R.id.tv_720p){
            levelCallBack.setVideoLevel(2);
        }else if(v.getId() == R.id.tv_360p){
            levelCallBack.setVideoLevel(3);
        }
    }

    public interface VideoLevelCallBack{
        void setVideoLevel(int Level);
    }

    public class LevelAdapter extends RecyclerView.Adapter<LevelViewHolder>{


        @NonNull
        @Override
        public LevelViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            LayoutInflater localinflater =(LayoutInflater)mContext.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            View itemView = localinflater.inflate(R.layout.level_item_layout,viewGroup,false);

            return new LevelViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LevelViewHolder levelViewHolder, int position) {
            Camera.Size mSize = previewSizes.get(position);
            levelViewHolder.levelTv.setText(mSize.width+"x"+mSize.height);
            levelViewHolder.levelTv.setTag(position);
            levelViewHolder.levelTv.setOnClickListener(v -> {
                if(levelCallBack == null) return;
                int tmpPosition = (int) v.getTag();
                levelCallBack.setVideoLevel(tmpPosition);
                QualityDialog.this.dismiss();
            });
        }

        @Override
        public int getItemCount() {
            return previewSizes.size();
        }
    }

    public class LevelViewHolder extends RecyclerView.ViewHolder{
        public TextView levelTv;

        public LevelViewHolder(@NonNull View itemView) {
            super(itemView);
            levelTv = itemView.findViewById(R.id.tv_level);
        }
    }

}
